package com.kbbywyclient.ui.activity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.AccountInfo;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.BankCardManageModule;
import com.kbbywyclient.presenter.BankCardManagePresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.contarct.BankCardManageContract;
import com.kbbywyclient.util.ImageLoaderUtils;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.widget.CustomToolBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jiangzx on 2017/9/7 19:55.
 */

public class BankCardManageActivity extends BaseActivity implements BankCardManageContract.View{
    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;

    @BindView(R.id.rl_card_info)
    RelativeLayout rlCardInfo;
    @BindView(R.id.civ_bank_logo)
    CircleImageView civBankLogo;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_type)
    TextView tvBankType;
    @BindView(R.id.card_holder_name)
    TextView tvCardHolderName;
    @BindView(R.id.tv_bank_card_number)
    TextView tvBankCardNumber;

    @Inject
    BankCardManagePresenter presenter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_bank_card;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        toolBar.setLeftViewOnClickLinster(v->finish());
    }

    @Override
    public void initData() {
        presenter.getUserBindCard(getUser(this).getSalesID());
    }

    @Override
    protected void onResume() {
        super.onResume();
        setScreenName("银行卡");
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getBankCardManageComponent(new BankCardManageModule(this)).inject(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCardBindSuccess(Event.CardBindSuccess cardSuccess){
        presenter.getUserBindCard(getUser(this).getSalesID());
    }

    @Override
    public void showLoadingDialog(String s) {
        showProgress(s);
    }

    @Override
    public void closeLoadingDialog() {
        dismissProgress();
    }

    @Override
    public void getDataSuccess(AccountInfo info) {
        if(info.getBankcardBindStatus()==1){
            //已绑卡
            rlCardInfo.setVisibility(View.VISIBLE);
            AccountInfo.BankCardInfo cardInfo=info.getBankcardInfo();
            ImageLoaderUtils.displayImage(civBankLogo, API.PIC_ADDRESS+cardInfo.getBank_logo());
            ViewUtils.setText(tvBankName,cardInfo.getBank_name());
            tvBankType.setText(cardInfo.getCard_type()==1?"储蓄卡":"信用卡");
            tvCardHolderName.setText(cardInfo.getAlipay_true_name());
            tvBankCardNumber.setText(String.format(getString(R.string.text_card_number_str),
                    cardInfo.getAlipay_account()));
            switch (cardInfo.getBack_color()){
                case 1:
                    //红色背景
                    rlCardInfo.setBackgroundResource(R.mipmap.img_card_bg_red);
                    break;
                case 2:
                    rlCardInfo.setBackgroundResource(R.mipmap.img_card_bg_yellow);
                    break;
                case 3:
                    rlCardInfo.setBackgroundResource(R.mipmap.img_card_bg_green);
                    break;
                case 4:
                    rlCardInfo.setBackgroundResource(R.mipmap.img_card_bg_blue);
                    break;
                default:
                    rlCardInfo.setBackgroundResource(R.mipmap.img_card_bg_purple);
                    break;
            }
        }
    }
}
